package display
{
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.PixelSnapping;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class ScaleHider extends Bitmap
	{
		private var _scale:Number = 0.2;
		private var _duration:Number;
		private var _endAlpha:Number;
		
		private var _running:Boolean = false;
		public function ScaleHider(duration:Number = 0.3, endAlpha:Number = 0.1)
		{
			super(null, PixelSnapping.AUTO, true);
			_duration = duration;
			_endAlpha = endAlpha;
		}
		
		public function show(display:DisplayObject):void
		{
			if(_running || display.stage == null)
				return;
			else
			{
				var bound:Rectangle = display.getBounds(display);
				var bmpData:BitmapData = new BitmapData(bound.width, bound.height, true, 0);
				
				var m:Matrix = new Matrix();
				m.createBox(1, 1, 0, -bound.x, -bound.y);
				bmpData.draw(display, m, null, BlendMode.LAYER, null, true);
				bitmapData = bmpData;
				
				var leftTop:Point = display.localToGlobal(new Point(bound.x, bound.y));
				display.stage.addChild(this);
				alpha = 1;
				scaleX = 1;
				scaleY = 1;
				x = leftTop.x;
				y = leftTop.y;
				display.stage.addChild(this);
				TweenLite.to(this, _duration, {"alpha":_endAlpha, "scaleX":_scale, "scaleY":_scale, "x":leftTop.x + width * (1 - _scale) / 2, "y":leftTop.y + height * (1 - _scale) / 2, 
				"onComplete":completeFunction});
				_running = true;
			}
		}
		
		private function completeFunction():void
		{
			stage.removeChild(this);
			_running = false;
		}
	}
}